<template>
  <div class="feedback-manage">
    <el-card>
      <div class="card-header">
        <h3>玩家反馈管理</h3>
        <div class="tools">
          <el-input
              v-model="searchQuery"
              placeholder="输入玩家ID或反馈内容搜索"
              clearable
              style="width: 250px"
              @keyup.enter="handleSearch"
          />
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </div>
      </div>

      <el-table
          :data="feedbacks.records"
          border stripe
          v-loading="loading"
          @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="100" sortable />
        <el-table-column prop="playerId" label="玩家ID" width="120" sortable />
        <el-table-column prop="feedbackContent" label="反馈内容" min-width="300" sortable />
        <el-table-column prop="isProcessed" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.isProcessed === 1 ? 'success' : 'danger'">
              {{ row.isProcessed === 1 ? '已处理' : '未处理' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button
                size="small"
                type="primary"
                @click="markAsProcessed(row.id)"
                v-if="row.isProcessed === 0"
            >标记为已处理</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <div class="pagination-container">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="feedbacks.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

// 分页状态
const currentPage = ref(1)
const pageSize = ref(10)
const searchQuery = ref('')
const loading = ref(false)

// 排序状态
const sortProp = ref('')
const sortOrder = ref('')

// 反馈数据
const feedbacks = ref({
  page: 1,
  rows: 10,
  total: 0,
  records: []
})

// 获取反馈列表
const fetchFeedbacks = async (page = currentPage.value, size = pageSize.value) => {
  try {
    loading.value = true
    const response = await request.get('/player/feedbacks/unprocessed', {
      params: {
        page,
        pageSize: size,
        keyword: searchQuery.value,
        sortField: sortProp.value,
        sortOrder: sortOrder.value === 'ascending' ? 'asc' : sortOrder.value === 'descending' ? 'desc' : ''
      }
    })
    feedbacks.value = response.data
  } catch (error) {
    console.error('获取反馈列表失败', error)
    ElMessage.error('获取反馈列表失败')
  } finally {
    loading.value = false
  }
}

// 处理页大小变化
const handleSizeChange = (size) => {
  pageSize.value = size
  fetchFeedbacks(1, size)
}

// 处理当前页变化
const handleCurrentChange = (page) => {
  currentPage.value = page
  fetchFeedbacks(page, pageSize.value)
}

// 搜索功能
const handleSearch = () => {
  currentPage.value = 1 // 搜索后重置到第一页
  fetchFeedbacks(1, pageSize.value)
}

// 处理排序变化
const handleSortChange = ({ prop, order }) => {
  sortProp.value = prop
  sortOrder.value = order
  currentPage.value = 1 // 重置到第一页
  fetchFeedbacks()
}

// 标记为已处理
const markAsProcessed = async (feedbackId) => {
  try {
    await request.put(`/player/feedback/${feedbackId}/process`)
    ElMessage.success('标记成功')
    fetchFeedbacks(currentPage.value, pageSize.value)
  } catch (error) {
    console.error('标记失败', error)
    ElMessage.error('操作失败')
  }
}

onMounted(() => {
  fetchFeedbacks()
})
</script>

<style scoped>
.feedback-manage {
  padding: 20px 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.tools {
  display: flex;
  gap: 10px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>